package com.sinopac.action.TokenManagement;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.CustomerApply.CustomerVO;
import com.sinopac.entity.CustomerApply.ReportVO;
import com.sinopac.entity.TokenManagement.TokenVO;
import com.sinopac.model.CustomerApply.CustomerApplyModel;
import com.sinopac.model.TokenManagement.TokenManagementModel;

@Namespace("/TokenManagement")
@ParentPackage("struts-default")
public class TokenManagementAction extends ActionCore{

	private static final long serialVersionUID = 1L;
	private Boolean pdfFlag = false;
	private String pdfPath;
	private TokenVO tokenVO;
	private String actionName;
	private String custId;
	private String token_esn;
	private String errorMessage;
	private Boolean errorFlag;
	private List<CustomerVO> customerList;
	private List<CustomerVO> GroupList;
	//report
	private List<ReportVO> reportList = new ArrayList<ReportVO>();
	
	@Action(value="index",results={@Result(name="input",location="/form/TokenManagement/TokenManagement.jsp")})
	public String execute(){
		errorFlag = false;
		TokenManagementModel tmm = new TokenManagementModel();
		tokenVO = tmm.getTokenList((String)session.get("token_esn"));
		tokenVO.setOperator((String)session.get("userName"));
		System.out.println("pdfFlag:" + pdfFlag);
		if(pdfFlag){
			CustomerApplyModel cam = new CustomerApplyModel();
			String token_esn = StringUtils.isNotBlank((String)session.get("token_esn"))?(String)session.get("token_esn"):this.token_esn;			
			cam.getReport(tokenVO.getCustVO().get(0).getCustomer_id(), token_esn, session.get("userName").toString(),reportList);
			pdfPath = cam.generatePDF(reportList);
		}
		return INPUT;
	}
	
	@Action(value="transactionToken",results={@Result(name="success",type="chain",location="index"),
											  @Result(name="error",location="/form/TokenManagement/TokenManagement.jsp")})
	public String transactionToken(){
		errorFlag = false;
		if(StringUtils.isNotBlank(actionName)){
			TokenManagementModel tmm = new TokenManagementModel();
			if(tmm.transactionToken((String)session.get("token_esn"), actionName,(String)session.get("userName"))){
				return SUCCESS;
			}
		}
		return ERROR;
	}
	
	@Action(value="allotmentToken",results={@Result(name="success",type="redirect",location="/CustomerApply/allotmentToken"),// @Result(name="success",location="/form/CustomerApply/tokenSuccess.jsp"),
											@Result(name="error",location="/form/TokenManagement/TokenManagement.jsp")})
	public String allotmentToken(){
		TokenManagementModel tmm = new TokenManagementModel();
		StringBuffer custIdMix = new StringBuffer();
		CustomerApplyModel cam = new CustomerApplyModel();
		System.out.println("tokenEsnTM:" + (String)session.get("token_esn"));
//		setToken_esn((String)session.get("token_esn"));
		if(StringUtils.isNotBlank(custId)){			
			// 客戶ID前面18位未滿的話補空白
			if(custId.length()<23){
				String[] strlist = custId.split("-");
				custIdMix.append(strlist[0].toString());
				for(int i=1; i<=18-strlist[0].length(); i++){
					custIdMix.append(" ");
				}
				custIdMix.append("-" + strlist[1].toString() + "-" + strlist[2].toString());
//				System.out.println("cusId1:"+custIdMix.toString());							
				session.put(CustomersID, custIdMix.toString());
				String message = tmm.chkCustId(custIdMix.toString(), (String)session.get("token_esn"));
				tokenVO = tmm.getTokenList((String)session.get("token_esn"));
				tokenVO.setOperator((String)session.get("userName"));
				if(StringUtils.isBlank(message)){
					customerList = cam.getCustomerForm(custIdMix.toString());
					GroupList = cam.getGroupList(custIdMix.toString());
					return SUCCESS;
				}else{
					errorFlag = true;
					errorMessage = message;
				}
			}else{
				session.put(CustomersID, custId);
				String message = tmm.chkCustId(custId, (String)session.get("token_esn"));
				tokenVO = tmm.getTokenList((String)session.get("token_esn"));
				tokenVO.setOperator((String)session.get("userName"));
				if(StringUtils.isBlank(message)){
					customerList = cam.getCustomerForm(custId);
					GroupList = cam.getGroupList(custId);
					return SUCCESS;
				}else{
					errorFlag = true;
					errorMessage = message;
				}
			}						
		}
		return ERROR;
	}
	
	@Action(value="linkCust",results={@Result(name="success",type="redirect",location="/CustomerApply/index")})
	public String linkCust(){
		if(StringUtils.isNotBlank(custId)){
			
		}
		return ERROR;
	}
	
	public TokenVO getTokenVO() {
		return tokenVO;
	}

	public void setTokenVO(TokenVO tokenVO) {
		this.tokenVO = tokenVO;
	}
	public String getActionName() {
		return actionName;
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getCustId() {
		return custId;
	}

	public String getToken_esn() {
		return token_esn;
	}

	public void setToken_esn(String token_esn) {
		this.token_esn = token_esn;
	}

	public void setCustId(String custId) {
		this.custId = custId;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public Boolean getErrorFlag() {
		return errorFlag;
	}

	public void setErrorFlag(Boolean errorFlag) {
		this.errorFlag = errorFlag;
	}

	public List<CustomerVO> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<CustomerVO> customerList) {
		this.customerList = customerList;
	}

	public List<CustomerVO> getGroupList() {
		return GroupList;
	}

	public void setGroupList(List<CustomerVO> groupList) {
		GroupList = groupList;
	}

	public Boolean getPdfFlag() {
		return pdfFlag;
	}

	public void setPdfFlag(Boolean pdfFlag) {
		this.pdfFlag = pdfFlag;
	}

	public String getPdfPath() {
		return pdfPath;
	}

	public void setPdfPath(String pdfPath) {
		this.pdfPath = pdfPath;
	}

	public List<ReportVO> getReportList() {
		return reportList;
	}

	public void setReportList(List<ReportVO> reportList) {
		this.reportList = reportList;
	}
	
}
